package com.flex.mobile.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.stereotype.Service;

import com.flex.mobile.dao.IGenericDAO;
import com.flex.mobile.model.TaxiOrder;

@Service("orderDao")
public class TaxiOrderDao implements IGenericDAO<TaxiOrder> {
   private final SimpleJdbcTemplate template;
   private final SimpleJdbcInsert insertContact;

   private final RowMapper<TaxiOrder> summaryRowMapper = new ParameterizedRowMapper<TaxiOrder>() {

      public TaxiOrder mapRow(ResultSet rs, int rowNum) throws SQLException {
         TaxiOrder order = new TaxiOrder();
         order.setId(rs.getInt("id"));
         return order;
      }
   };

   @Autowired
   public TaxiOrderDao(DataSource dataSource) {
      this.template = new SimpleJdbcTemplate(dataSource);
      this.insertContact = new SimpleJdbcInsert(dataSource).withTableName(
            "TAXIORDER").usingGeneratedKeyColumns("ID");
   }

   @Override
   public List<TaxiOrder> findAll() {
      return this.template
            .query(
                  "SELECT * FROM TAXIORDER",
                  this.summaryRowMapper);
   }

   @Override
   public List<TaxiOrder> findByName(String name) {
      // TODO Auto-generated method stub
      return null;
   }

   @Override
   public TaxiOrder findById(long id) {
      // TODO Auto-generated method stub
      return null;
   }

   @Override
   public TaxiOrder create(TaxiOrder order) {
      Map<String, Object> parameters = new HashMap<String, Object>();
      Number id = this.insertContact.executeAndReturnKey(parameters);
      order.setId(id.intValue());
      return order;
   }

   @Override
   public boolean update(TaxiOrder item) {
      // TODO Auto-generated method stub
      return false;
   }

   @Override
   public boolean remove(TaxiOrder item) {
      // TODO Auto-generated method stub
      return false;
   }

}
